package com.wuji1626.spark.rdd.transform

import org.apache.spark.{SparkConf, SparkContext}

object RDD_Transform_aggregateByKey_synthesize {

  def main(args: Array[String]): Unit = {
    // Step1: 准备环境
    val sparkConf = new SparkConf().setMaster("local[*]").setAppName("Operator")
    val sc = new SparkContext(sparkConf)
    // Step2: 聚合算子综合
    // 数据分区【("a",1),("a",2)】【("a",3),("a",4)】
    val rdd = sc.makeRDD(List(("a",1),("a",2),("b",3),("b",4),("b",5),("a",6)),numSlices = 2)
    rdd.reduceByKey(_+_)  // 计算 wordCount
    rdd.aggregateByKey(zeroValue = 0)(_+_, _+_) // 计算 wordCount
    rdd.foldByKey(zeroValue = 0)(_+_) // 计算 wordCount
    rdd.combineByKey(v=>v,(x:Int, y)=>x+y,(x:Int,y:Int)=>x+y)// 计算 wordCount

    // Step3: 关闭环境
    sc.stop()
  }
}
